﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using LuckyLoot.Common.Repositories;
using LuckyLoot.Domain.Entities.People;
using LuckyLoot.Domain.Entities;

namespace LuckyLoot.Common.Contexts
{
    public class ApplicationContextInitializer : DropCreateDatabaseIfModelChanges<ApplicationContext>
    {
        protected override void Seed(ApplicationContext context)
        {
            this.CreateUsers(context);
            this.CreateCustomers(context);
            this.CreateSuppliers(context);
            this.CreateSupplies(context);
            this.CreateTools(context);
        }

        private void CreateCustomers(ApplicationContext context)
        {
            var customerRepository = new Repository<Customer, ApplicationContext>();

            Customer caverna = new Customer();
            caverna.Name = "Caverna";
            caverna.Facebook = "http://facebook.com/corvina";
            caverna.Email = "carlos.pnx@hotmail.com";

            Customer martin = new Customer();
            martin.Name = "Martin";
            martin.Facebook = "http://facebook.com/martinsom";
            martin.Email = "martinsom666@hotmail.com";

            Customer sano = new Customer();
            sano.Name = "Sano";
            sano.Facebook = "http://facebook.com/imme";
            sano.Email = "tiago.imme@gmail.com";

            customerRepository.Add(caverna);
            customerRepository.Add(martin);
            customerRepository.Add(sano);

            customerRepository.Save();
        }

        private void CreateUsers(ApplicationContext context)
        {
            User mineiro = new User();
            mineiro.Name = "Mineiro";
            mineiro.Email = "mineiro@bol.com.br";
            mineiro.Password = "mineiro";
            mineiro.Phone = "97796446";

            TattooArtist erasmo = new TattooArtist();
            erasmo.Name = "Erasmo De Zutter Júnior";
            erasmo.Email = "luckyloot@ig.com.br";
            erasmo.Password = "luckyloot";
            erasmo.Phone = "96695885";
            erasmo.IsAdmin = true;
            erasmo.Facebook = "www.facebook.com/luckyloottattoo";

            List<User> users = new List<User>();
            users.Add(mineiro);
            users.Add(erasmo);

            using (Repository<User, ApplicationContext> repository = new Repository<User, ApplicationContext>())
            {
                foreach (User user in users)
                {
                    repository.Add(user);
                }

                repository.Save();
            }
        }

        private void CreateSuppliers(ApplicationContext context)
        {
            Supplier electricInk = new Supplier();
            electricInk.Name = "Electric Ink";
            electricInk.ServiceDescription = "Fornecedor de tintas para tatuagem.";
            electricInk.Phone = "99856274";
            electricInk.WebSite = "http://electricink.com.br/";
            electricInk.Email = "contato@electricink.com.br";
            electricInk.LatestPurchase = new DateTime(2012, 05, 16);

            using (Repository<Supplier, ApplicationContext> repository = new Repository<Supplier, ApplicationContext>())
            {
                repository.Add(electricInk);
                repository.Save();
            }
        }

        private void CreateSupplies(ApplicationContext context)
        {
            Supply tintaPreta = new Supply();
            tintaPreta.Name = "Tinta preta 30ml Electric Ink";
            tintaPreta.Amount = 3;
            tintaPreta.ContentAmount = 30;
            tintaPreta.Acquisition = new DateTime(2012, 02, 15);
            tintaPreta.Expiration = new DateTime(2013, 02, 15);
            tintaPreta.MeasureType = MeasureType.Milliliter;
            tintaPreta.Type = SupplyType.TattooSupply;

            Supply agulha1 = new Supply();
            agulha1.Name = "Agulha para contorno - 5 pontas";
            agulha1.Amount = 15;
            agulha1.Acquisition = new DateTime(2012, 02, 15);
            agulha1.Expiration = null;
            agulha1.ContentAmount = 1;
            agulha1.MeasureType = MeasureType.Unity;
            agulha1.Type = SupplyType.TattooSupply;

            Supply agulha2 = new Supply();
            agulha2.Name = "Agulha para preenchimento - 12 pontas";
            agulha2.Amount = 25;
            agulha2.ContentAmount = 1;
            agulha2.Acquisition = new DateTime(2012, 02, 15);
            agulha2.Expiration = null;
            agulha2.MeasureType = MeasureType.Unity;
            agulha2.Type = SupplyType.TattooSupply;

            Supply luvas = new Supply();
            luvas.Name = "Luvas descartáveis";
            luvas.Amount = 10;
            luvas.ContentAmount = 100;
            luvas.Acquisition = new DateTime(2012, 02, 15);
            luvas.Expiration = null;
            luvas.MeasureType = MeasureType.Unity;
            luvas.Type = SupplyType.TattooSupply;

            Supply papelToalha = new Supply();
            papelToalha.Name = "Papel Toalha";
            papelToalha.Amount = 4;
            papelToalha.ContentAmount = 50;
            papelToalha.Acquisition = new DateTime(2012, 02, 15);
            papelToalha.Expiration = null;
            papelToalha.MeasureType = MeasureType.Meter;
            papelToalha.Type = SupplyType.TattooSupply;

            Supply filmePVC = new Supply();
            filmePVC.Name = "Filme de pvc";
            filmePVC.Amount = 2;
            filmePVC.ContentAmount = 100;
            filmePVC.Acquisition = new DateTime(2012, 02, 15);
            filmePVC.Expiration = null;
            filmePVC.MeasureType = MeasureType.Meter;
            filmePVC.Type = SupplyType.TattooSupply;

            Supply copoDeTinta = new Supply();
            copoDeTinta.Name = "Copo para tinta";
            copoDeTinta.Amount = 1;
            copoDeTinta.ContentAmount = 300;
            copoDeTinta.Acquisition = new DateTime(2012, 02, 15);
            copoDeTinta.Expiration = null;
            tintaPreta.MeasureType = MeasureType.Unity;
            tintaPreta.Type = SupplyType.TattooSupply;

            using (Repository<Supply, ApplicationContext> repository = new Repository<Supply, ApplicationContext>())
            {
                repository.Add(tintaPreta);
                repository.Add(agulha1);
                repository.Add(agulha2);
                repository.Add(luvas);
                repository.Add(papelToalha);
                repository.Add(filmePVC);
                repository.Add(copoDeTinta);

                repository.Save();
            }
        }

        private void CreateTools(ApplicationContext context)
        {
            Tool maquinaDeContorno = new Tool();
            maquinaDeContorno.Name = "Máquina de tatuagem para contorno";

            Tool maquinaDePreenchimento = new Tool();
            maquinaDePreenchimento.Name = "Máquina de tatuagem para preenchimento";

            Tool biqueira = new Tool();
            biqueira.Name = "Biqueira para preenchimento com grip 28mm";
            biqueira.LastSterilization = new DateTime(2013, 02, 15);

            using (Repository<Tool, ApplicationContext> repository = new Repository<Tool, ApplicationContext>())
            {
                repository.Add(biqueira);
                repository.Add(maquinaDePreenchimento);
                repository.Add(maquinaDeContorno);

                repository.Save();
            }
        }
    }
}
